//
//  UpdataAlertView.m
//  MeterMart
//
//  Created by ENERGY on 2018/11/2.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "UpdataAlertView.h"
@interface UpdataAlertView()
@property (nonatomic, strong)NSString * updateURL;
@end
@implementation UpdataAlertView(networking)
- (void)updateURL{
    [[AFAppDotNetAPIClient sharedClient] POST:@"indexApi/update_ios" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
//            NSString * urlStr = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",@"1440479642"];
//
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
//            [self cancelView];
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

@end
@implementation UpdataAlertView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
        [backBtn addTarget:self action:@selector(hidden:) forControlEvents:UIControlEventTouchUpInside];
        backBtn.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backBtn];
        //白色背景
        UIView * whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.layer.cornerRadius = 8;
        whiteView.layer.masksToBounds = YES;
        [self addSubview:whiteView];
        //标题
        UILabel * titleLab = [[UILabel alloc] init];
        titleLab.text = @"检测更新";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.font = DEF_FontSize_18;
        titleLab.textColor = [UIColor blackColor];
        [whiteView addSubview:titleLab];
        //提示内容
        UILabel * contentLab = [[UILabel alloc] init];
        contentLab.text = @"检测到当前版本并非最新版本，请你尽快更新，享受更周到的服务";
        contentLab.textAlignment = NSTextAlignmentLeft;
        contentLab.font = DEF_FontSize_14;
        contentLab.lineBreakMode = 0;
        contentLab.numberOfLines = 0;
        contentLab.textColor = [UIColor blackColor];
        [whiteView addSubview:contentLab];
        //立即更新
        UIButton * updataBtn = [[UIButton alloc] init];
        [updataBtn setTitle:@"立即更新" forState:UIControlStateNormal];
        [updataBtn setBackgroundColor:getColor(@"221815")];
        updataBtn.titleLabel.font = DEF_FontSize_15;
        [updataBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        updataBtn.layer.cornerRadius = 6;
        updataBtn.layer.masksToBounds = YES;
        [updataBtn addTarget:self action:@selector(updataBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:updataBtn];
        //masonry
        __weak typeof (self)weakSelf = self;
        [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(40/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.mas_right).mas_offset(-40/WIDTH_6_SCALE);
            make.centerY.equalTo(weakSelf.mas_centerY).mas_offset(-40/WIDTH_6_SCALE);
            make.height.mas_offset(228/WIDTH_6_SCALE);
        }];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(whiteView.mas_left).mas_offset(26/WIDTH_6_SCALE);
            make.top.equalTo(whiteView.mas_top).mas_offset(43/WIDTH_6_SCALE);
        }];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).mas_offset(27/WIDTH_6_SCALE);
            make.right.equalTo(whiteView.mas_right).mas_offset(-26/WIDTH_6_SCALE);
            make.left.equalTo(whiteView.mas_left).mas_offset(26/WIDTH_6_SCALE);
        }];
        [updataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(whiteView.mas_bottom).mas_offset(-33/WIDTH_6_SCALE);
            make.centerX.equalTo(whiteView.mas_centerX);
            make.width.mas_offset(161/WIDTH_6_SCALE);
            make.height.mas_offset(40/WIDTH_6_SCALE);
        }];
    }return self;
}

- (void)showView{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)updataBtnClick:(UIButton *)sender{
    NSLog(@"更新");
    NSString * urlStr = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",@"1440479642"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
    [self cancelView];
}
- (void)hidden:(UIButton *)sender{
    NSLog(@"隐藏");
    [self cancelView];
}
- (void)cancelView{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
